<template>
  <div class="dict">
    <div class="dictList">
      <div class="list-div">
        <el-row>
          <el-col :lg="4" :md="6">
            <el-button type="success" size="small" plain @click="handleSave('saveModal')">新 增</el-button>
            <el-button type="danger" size="small" plain @click="deleteBatch()">删 除</el-button>
          </el-col>
          <el-col :lg="10" :md="10">
            <el-col :lg="10" :md="10">
              <el-input
                size="small"
                @keyup.enter.native="search"
                placeholder="会议室名称"
                v-model="searchFilter.condition"
                clearable
              ></el-input>
            </el-col>
            <el-col :offset="1" :span="6">
              <el-button size="small" type="primary" @click="search" plain>查 询</el-button>
            </el-col>
          </el-col>
        </el-row>

        <br>
        <el-table
          :data="tableData"
          border
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="40" header-align="center"></el-table-column>
          <el-table-column type="index" width="40" header-align="center"></el-table-column>

          <el-table-column sortable prop="name" label="名称" align="center"></el-table-column>
          <el-table-column sortable prop="createTime" align="center" label="创建时间"></el-table-column>
          <el-table-column sortable prop="creator" align="center" label="创建人"></el-table-column>
          <el-table-column fixed="right" label="操作" align="center">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click.native="handleEdit(scope.row)">编辑</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination block">
          <el-pagination
            background
            @current-change="handleCurrentChange"
            :current-page.sync="page"
            :page-size="pageSize"
            layout="total, prev, pager, next"
            :total="total"
          ></el-pagination>
        </div>
      </div>
    </div>
    <div class="saveModal">
      <el-dialog :title="saveName" :visible.sync="showSaveModal" @close="cancelR" width="45%">
        <el-form
          :rules="saveModalRules"
          ref="saveModal"
          :model="saveModal"
          label-width="120px"
          size="small"
        >
          <br>
          <el-row>
            <el-col :span="20">
              <el-form-item label="名称" prop="name">
                <el-input v-model="saveModal.name"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <br>
          <el-row>
            <el-col :span="20">
              <el-form-item size="large" style="text-align: center">
                <el-button type="primary" @click="save('saveModal')">{{saveBtnName}}</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  name: "Role",
  data() {
    return {
      saveName: "",
      resourceCheckList: [1],
      showSetResourceModal: false,
      codeInputEnable: false,
      searchFilter: {
        condition: ""
      },
      saveBtnName: "创建字典",
      showSaveModal: false,
      setResourceRoleId: "",
      tableData: [],
      page: 1,
      pageSize: 10,
      total: 100,
      totalPage: 0,
      orgId: null,
      multipleSelection: [],
      saveModalRules: {
        name: [
          {
            required: true,
            message: "请输入会议室名称",
            trigger: "blur"
          }
        ]
      },
      saveModal: {
        name: "" //会议室名称
      },
      tmpSaveModal: {
        name: "" //会议室名称
      },
      deleteList: [] //选中的需要删除的id集合
    };
  },
  mounted() {
    this.handleNodeClick();
  },
  methods: {
    //根据条件查询列表
    search() {
      let qColumn;
      let qValue;
      if (
        this.searchFilter.condition != null &&
        this.searchFilter.condition.length > 0
      ) {
        qColumn = "AND_name_LK";
        qValue = this.searchFilter.condition;
      }
      this.http
        .post("/api/boardroom/query", {
          P_NO: this.page,
          P_SIZE: this.pageSize,
          P_ORDER: "DESC",
          P_SORT: "create_time",
          Q_COLUMN: qColumn != null ? qColumn : "",
          Q_VALUE: qValue != null ? qValue : ""
        })
        .then(res => {
          var data = res.data;
          this.tableData = data.rows;
          this.total = data.records;
        });
    },

    //新增会议室
    handleSave(formName) {
      this.tmpSaveModal.name = "";
      this.saveBtnName = "创建字典";
      this.saveModal = this.tmpSaveModal;
      this.saveName = "创建会议室";
      this.showSaveModal = true;
      this.codeInputEnable = false;
    },

    handleCurrentChange(val) {
      this.page = val;
      this.handleNodeClick();
    },
    //关闭弹框右上角
    cancelR() {
      this.$refs.saveModal.clearValidate();
    },
    //编辑
    handleEdit(row) {
      this.saveModal = row;
      this.saveName = "编辑会议室";
      this.showSaveModal = true;
      this.codeInputEnable = true;
      this.saveBtnName = "保存字典";
    },
    //保存
    save(formName) {
      let _this = this;
      this.$refs[formName].validate(valid => {
        if (!valid) {
          console.log("error submit!!");
          return false;
        } else {
          this.http.post("/api/boardroom/save", this.saveModal).then(res => {
            let data = res.data;
            if (data.code === 200) {
              this.$message({
                showClose: true,
                message: data.msg,
                type: "success"
              });
              _this.showSaveModal = false;
              _this.handleNodeClick();
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    //批量删除
    deleteBatch() {
      let selectDataList = this.multipleSelection;
      if (selectDataList.length == 0) {
        this.$message({
          message: "请选中一条项再操作",
          type: "warning"
        });
        return;
      }
      let ids = "";
      for (let i = 0; i < selectDataList.length; i++) {
        if (i != selectDataList.length - 1) {
          ids += selectDataList[i].id + ",";
        } else {
          ids += selectDataList[i].id;
        }
      }
      let deleteList = this.deleteList;
      selectDataList.map(item => {
        deleteList.push(item.id);
      });

      this.$confirm("此操作将永久删除选中的数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.http
          .post("/api/boardroom/deleteBatchIds", {
            ids: ids
          })
          .then(res => {
            let data = res.data;
            if (data.code === 200) {
              this.$message({
                showClose: true,
                message: data.msg,
                type: "success"
              });
              //如果在尾页删除了所有的数据，将page更改成前一页
              if (this.page == this.totalPage) {
                if (this.deleteList.length == this.total % this.pageSize) {
                  this.page = this.totalPage - 1;
                }
              }
              this.handleNodeClick();
              this.deleteList = [];
            } else {
              this.$message.error(data.msg);
            }
          });
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //查询列表
    handleNodeClick() {
      this.http
        .post("/api/boardroom/query", {
          P_NO: this.page,
          P_SIZE: this.pageSize,
          P_ORDER: "DESC",
          P_SORT: "create_time"
        })
        .then(res => {
          var data = res.data;
          this.total = data.records;
          this.totalPage = data.totalPage;
          this.tableData = data.rows;
        });
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
.demo-table-expand {
  font-size: 0;
}

.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}

.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}

.dict {
  padding: 0 10px;
  .pagination {
    float: right;
  }
}

.face-div {
  margin: 0 auto;
  align-items: center;
  justify-content: center;
  text-align: center;

  .item {
    display: inline-block;
    box-shadow: 0 0 4px black;
  }

  img {
    width: 200px;
    height: auto;
  }
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
